import {
  Vector2,
  CurvePath,
  LineCurve3,
  Vector3,
  ExtrudeGeometry,
  MeshBasicMaterial,
  Mesh,
} from "three";
import { Loft3DMesh } from "../types/DrawingGroups/meshs";
import { KujialeParamModel } from "../types/KujialeJsonModels/KujialeParamModel";
import { getShape } from "./path";
import type { ProfileItem } from "../types/Libraries/Profiles";
import { type DrawContext } from "./context";
import { LoftModel } from "../types/Models";
import { DrawMeshWireframeLines } from "./wireframe";

export const DrawLoft3DMesh = (
  json: KujialeParamModel,
  context: DrawContext
): Mesh | undefined => {
  //获取路径
  const { resultLines: lines, resultPoints: points } = json.paramLoftPath!;

  //检查截面
  const filteredProfile = context.profiles.find(
    (it) => it.name == json.profile?.brandGoodName
  );

  if (filteredProfile === undefined) {
    const message = "未找到profile的截面数据:" + json.profile!.brandGoodName;
    if (context.logs.find((m) => m.message === message) === undefined) {
      context.logs.push({
        level: 3,
        message: message,
      });
      console.log("缺少profile的模型", json);
    }

    return undefined;
  }

  const shape = getShape(
    filteredProfile!.points.map((p) => new Vector2(p.x, p.y)),
    filteredProfile!.lines.map(() => {
      return {
        type: 0,
      };
    })
  );

  //将路径转换为可以拉伸的curve
  const [startpt, ...endpt] = points;
  const curve = new CurvePath();
  for (let i = 1; i < points.length; i++) {
    var lastpt = points[i - 1];
    var currentpt = points[i];
    curve.curves.push(
      new LineCurve3(
        new Vector3(lastpt.x, -lastpt.y, 0),
        new Vector3(currentpt.x, -currentpt.y, 0)
      )
    );
  }

  //处理闭合
  if (json.paramLoftPath?.close && points.length > 2) {
    var fstPt = new Vector2();
    fstPt.copy(points[0]);
    var lstPt = new Vector2();
    lstPt.copy(points[points.length - 1]);

    if (fstPt.distanceTo(lstPt) > 1) {
      curve.curves.push(
        new LineCurve3(
          new Vector3(lstPt.x, -lstPt.y, 0),
          new Vector3(fstPt.x, -fstPt.y, 0)
        )
      );
    }
  }

  const geometry = new ExtrudeGeometry(shape, {
    extrudePath: curve, //CurvePath是可以用的，不用理会报错
    steps: 300,
  });
  const mesh = new Loft3DMesh(new LoftModel(json));
  mesh.geometry = geometry;
  mesh.material = new MeshBasicMaterial({
    color: context.colors[json.textureName],
  });
  mesh.rotateOnWorldAxis(new Vector3(1, 0, 0), Math.PI / 2);
  mesh.rotateAndMoveByLoftModel();
  //绘制线框
  DrawMeshWireframeLines(mesh, true, context.colors["wireframe"]);
  return mesh;
  // mesh.geometry = geometry;
  // mesh.rotateAndMoveBy3D();
  // const [startpt, endpt] = points;
  // const curve = new LineCurve3(new Vector3(), new Vector3(0, -endpt.y, 0));
  // const geometry = new ExtrudeGeometry(shape, {
  //   extrudePath: curve,
  //   steps: 100,
  // });
  // mesh.geometry = geometry;
  // mesh.rotateOnWorldAxis(new Vector3(1, 0, 0), Math.PI / 2);
  // mesh.rotateAndMoveByLoftModel();

  // mesh.material = new MeshBasicMaterial({
  //   color: colorDict[json.textureName],
  // });

  // if (points.length > 2) {
  //   //万林 45度斜门板
  //   const shape = getShape(
  //     [
  //       new Vector2(0, 17),
  //       new Vector2(20, 17),
  //       new Vector2(20, 0),
  //       new Vector2(0, 17),
  //     ],
  //     [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }]
  //   );

  //   const [startpt, ...endpt] = points;
  //   const curve = new CurvePath();
  //   for (let i = 0; i < points.length; i++) {
  //     var lastpt = i == 0 ? points[points.length - 1] : points[i - 1];
  //     var currentpt = points[i];
  //     curve.curves.push(
  //       new LineCurve3(
  //         new Vector3(lastpt.x, -lastpt.y, 0),
  //         new Vector3(currentpt.x, -currentpt.y, 0)
  //       )
  //     );
  //   }
  //   curve.autoClose = true;
  //   const geometry = new ExtrudeGeometry(shape, {
  //     extrudePath: curve,
  //     steps: 300,
  //   });
  //   mesh.geometry = geometry;
  //   mesh.rotateOnWorldAxis(new Vector3(1, 0, 0), Math.PI / 2);
  //   mesh.rotateAndMoveByLoftModel();

  //   // const geometry = new BoxGeometry(
  //   //   json.size!["x"],
  //   //   json.size!["z"],
  //   //   json.size!["y"]
  //   // );
  //   // mesh.geometry = geometry;
  //   // mesh.rotateAndMoveBy3D();
  // } else {
  // }
};
